package com.one.efaimaly.main.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.RouterPath;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.StringUtils;
import com.one.common.view.base.IListView;
import com.one.common.view.pagestate.listpage.BaseListActivity;
import com.one.common.view.widget.MyTitleBar;
import com.one.common.model.bean.CityBean;
import com.one.efaimaly.R;
import com.one.efaimaly.main.model.item.PingyinItem;
import com.one.efaimaly.main.presenter.SelectCityPresenter;
import com.one.efaimaly.main.ui.binder.CityBinder;

/**
 * creator: zhulunjun
 * time:    2018/8/6
 * describe:
 */
@Route(path = RouterPath.SELECT_CITY)
public class SelectCityActivity extends BaseListActivity<SelectCityPresenter> implements IListView ,CityBinder.OnCitySelectListener, TextWatcher {
    @Override
    protected void initPresenter() {
        mPresenter = new SelectCityPresenter(this, this);
    }

    @Override
    public void registerDate() {
        register(CityBean.class, new CityBinder(this,this));
        register(PingyinItem.class, new PingYinBinder());
    }

    @Override
    public void loadData() {
        mPresenter.getCitys();
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("选择定位城市");
    }

    @Override
    public void initView() {
        super.initView();
        hindLoadMore();
        hindRefresh();
        addTopView(R.layout.layout_select_city_top);
        EditText etSearch = successView.findViewById(R.id.et_search);
        etSearch.addTextChangedListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        loadData();
    }

    @Override
    public void onCitySelect(CityBean bean) {
        Intent intent =new Intent();
        intent.putExtra(DefaultExtra.getExtraName(), bean.getName());
        setResult(RESULT_OK, intent);
        finishPage();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(v.getId() == R.id.tv_re_location){
            mPresenter.location();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(StringUtils.isNotBlank(charSequence.toString())) {
            mPresenter.filterData(charSequence.toString());
        } else {
            mPresenter.defaultData();
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}
